//异步处理或逻辑判断
import {ADD_PRODUCT_COUNTER, ADD_PRODUCT_TO_CART} from "@/store/mutation-types";

export default {
  addCart(context, payload) {
    return new Promise((resolve,reject) => {
      let oldProdcut = null;
      //方法一：
      /*for (let item of state.cartList) {
        if (item.iid === payload.iid) {
          oldProdcut = item;
          break;
        }
      }*/
      oldProdcut = context.state.cartList.find(item => {
        return item.iid === payload.iid;
      })
      if (oldProdcut) {
        //购物车存在该商品，数量+1
        context.commit(ADD_PRODUCT_COUNTER, oldProdcut);
        resolve('添加成功，当前商品数量+1')
      } else {
        //购物车不存在该商品，直接添加到购物车
        context.commit(ADD_PRODUCT_TO_CART, payload);
        resolve('添加成功')
      }

    })
  }
}
